package cn.yudao.product.service.impl;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.BaiduAuditUtils;
import cn.yudao.basic.utils.BaiduImgUtils;
import cn.yudao.basic.utils.FastdfsUtil;
import cn.yudao.basic.utils.FileUtil;
import cn.yudao.pet.domain.Pet;
import cn.yudao.pet.domain.PetDetail;
import cn.yudao.pet.domain.PetOnlineAuditLog;
import cn.yudao.pet.domain.PetType;
import cn.yudao.product.domain.Product;
import cn.yudao.product.domain.ProductDetail;
import cn.yudao.product.domain.ProductOnlineAuditLog;
import cn.yudao.product.domain.ProductPay;
import cn.yudao.product.dto.ProductPayDto;
import cn.yudao.product.mapper.ProductDetailMapper;
import cn.yudao.product.mapper.ProductMapper;
import cn.yudao.product.mapper.ProductOnlineAuditLogMapper;
import cn.yudao.product.mapper.ProductPayMapper;
import cn.yudao.product.service.IProductService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import cn.yudao.user.domain.User;
import cn.yudao.user.mapper.UserMapper;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Autowired
    private UserMapper userMapper;


    @Autowired
    private ProductPayMapper productPayMapper;

    @Override
    public ProductDetail loadDetailByPetId(Long id) {
        return productMapper.loadDetailByPetId(id);
    }

    @Override
    public void rutchRom(Long[] ids) {
        productMapper.rutchRom(ids);
        productDetailMapper.rutchRom(ids);

    }

    @Override
    public void onSalePet(Long[] ids) {
        // 查询数据库中所有想要上架的其中还没有上架的商品信息 进行百度ai校验
        List<Product> petlist = productMapper.loadOnSalePet(ids);
        for (Product pet : petlist) {
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                throw new BusinessException("文字违规");
            }
            String resources = pet.getResources();
            if (!StringUtils.isEmpty(resources)){

                String fileUrl = "http://123.207.27.208" + pet.getResources();
                File file = new File(fileUrl);
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {
                    MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                            ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                    Boolean aBoolean = BaiduImgUtils.ImgCensor(multipartFile);
                    System.out.println(aBoolean);
                    if (aBoolean){
                        ProductOnlineAuditLog petOnlineAuditLog = new ProductOnlineAuditLog();
                        petOnlineAuditLog.setProductId(pet.getId());
                        petOnlineAuditLog.setState(0);
                        petOnlineAuditLog.setNote("图片违规:" + fileUrl);
                        productOnlineAuditLogMapper.save(petOnlineAuditLog);
                        throw new BusinessException("图片违规");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
// 批量新增
        productMapper.onSalePet(ids);

    }

    @Override
    public void offSalePet(Long[] ids) {
        productMapper.offSalePet(ids);
    }



    @Override
    public void payProduct(ProductPayDto productPayDto) {
        Long productId = productPayDto.getProductId();
        BigDecimal price = productPayDto.getPrice();
        Long userId = productPayDto.getUserId();
        if (productId == null || price == null)
            throw new BusinessException("系统出了点小问题");

        User user = userMapper.loadById(userId);
        // 获取到当前服务
        Product product = productMapper.loadById(productPayDto.getProductId());
        // 创建一个任务
        ProductPay productPay = new ProductPay();
        // 赋值
        productPay.setPrice(price);
        productPay.setName(product.getName());
        productPay.setUserId(userId);
        productPay.setAddress(user.getPhone());
        productPay.setState(0);
        productPay.setProId(productId);
        productPayMapper.save(productPay);

    }


    @Override
    public void save(Product product) {
        productMapper.save(product);
        // 判断是否合规
        if (BaiduAuditUtils.TextCensor(product.getName())){

        }
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.save(detail);
    }


    @Override
    public void upadte(Product product) {
        productMapper.upadte(product);
        // 调用方法修改pet相关表
        updatePetRelated(product);
        super.upadte(product);
    }


    public void updatePetRelated(Product product){
        ProductDetail detail = product.getDetail();
        // 修改pet的详情
        productDetailMapper.upadte(detail);
    }


    @Override
    public void removeById(Long id) {
        if (id != null){
            productMapper.removeById(id);
            productDetailMapper.removeProduct(id);
        }
    }


}
